package my.wtp;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * Servlet implementation class Hello
 */
public class MyProxy extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static String url = "http://www.apache.org/";

	/**
	 * Default constructor.
	 */
	public MyProxy() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Create an instance of HttpClient.
		HttpClient client = new HttpClient();
		Properties property = new Properties();
		InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/mystuff.properties");
		property.load(stream);
		String expresso_url = property.getProperty("expresso_url");
		// Create a method instance.
		//String endPoint = "http%3A//localhost%3A8888/MyStuffProxy/Mockxi%3Fupdate-min%3Dabc123%26sort-by%3D456cfd";
		String updated = request.getParameter("updated-min");
		String endPoint = expresso_url + request.getParameter("endpoint");
		if (updated == null || updated.trim().length() == 0) {
		} else {
			endPoint += "?updated-min=" + updated;
		}
		
		System.out.println(endPoint);
		GetMethod method = new GetMethod(endPoint);

		// Provide custom retry handler is necessary
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler(3, false));

		try {
			// Execute the method.
			int statusCode = client.executeMethod(method);

			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
			}

			// Read the response body.
			byte[] responseBody = method.getResponseBody();

			// Deal with the response.
			// Use caution: ensure correct character encoding and is not binary
			// data
			response.getOutputStream().write(responseBody);

		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			method.releaseConnection();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(request, response);
	}

}
